import type {App} from 'vue';
import {createRouter, createWebHistory} from 'vue-router';
import routes from '@/router/modules/routes';
import {useCache} from "@/hooks/web/useCache";
import CacheKeys from "@/constants/CacheKeys";
import {useUserStore} from "@/store/modules/useUserStore";
import {usePermissionStore} from "@/store/modules/usePermissionStore";

const {wsCache} = useCache();
// 创建路由实例
const router = createRouter({
    history: createWebHistory(), // createWebHashHistory URL带#，createWebHistory URL不带#
    strict: true,
    routes,
    scrollBehavior: () => ({left: 0, top: 0}),
});

// 路由守卫
router.beforeEach((to, from, next) => {
    console.log("路由守卫")
    if (to.path == '/login' || wsCache.get(CacheKeys.USER_INFO)) {
        next();
    } else {
        alert('您还没有登录，请先登录');
        next('/login');
    }
})

export const setupRouter = (app: App<Element>) => {
    app.use(router);
};
export default router;
